package com.zero.lite.dao.entity;

import java.io.Serializable;

import com.baomidou.mybatisplus.enums.IdType;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableName;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 
 * </p>
 *
 * @author zero
 * @since 2018-05-04
 */
@TableName("lite_menu")
public class Menu extends Model<Menu> {

    private static final long serialVersionUID = 1L;

	@TableId(value="menu_id", type= IdType.AUTO)
	private Integer menuId;
	@TableField("menu_pid")
	private Integer menuPid;
	private String path;
	private String component;
	private String name;
	private String title;
	private String icon;
	private String redirect;
	private Integer level;
    /**
     * 0非菜单1菜单
     */
	@TableField("is_menu")
	private Integer isMenu;
    /**
     * 排序
     */
	private Integer sort;
    /**
     * 0非展开1展开
     */
	@TableField("is_open")
	private Integer isOpen;

	@TableField(exist = false)
	private List<Menu> children;
	@TableField(exist = false)
	private List<Menu> auth;

	public Integer getMenuId() {
		return menuId;
	}

	public void setMenuId(Integer menuId) {
		this.menuId = menuId;
	}

	public Integer getMenuPid() {
		return menuPid;
	}

	public void setMenuPid(Integer menuPid) {
		this.menuPid = menuPid;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getComponent() {
		return component;
	}

	public void setComponent(String component) {
		this.component = component;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getRedirect() {
		return redirect;
	}

	public void setRedirect(String redirect) {
		this.redirect = redirect;
	}

	public Integer getLevel() {
		return level;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}

	public Integer getIsMenu() {
		return isMenu;
	}

	public void setIsMenu(Integer isMenu) {
		this.isMenu = isMenu;
	}

	public Integer getSort() {
		return sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}

	public Integer getIsOpen() {
		return isOpen;
	}

	public void setIsOpen(Integer isOpen) {
		this.isOpen = isOpen;
	}

	public List<Menu> getChildren() {
		return children;
	}

	public void setChildren(List<Menu> children) {
		this.children = children;
	}

	public List<Menu> getAuth() {
		return auth;
	}

	public void setAuth(List<Menu> auth) {
		this.auth = auth;
	}

	@Override
	protected Serializable pkVal() {
		return this.menuId;
	}

	@Override
	public String toString() {
		return "Menu{" +
			"menuId=" + menuId +
			", menuPid=" + menuPid +
			", path=" + path +
			", component=" + component +
			", name=" + name +
			", title=" + title +
			", icon=" + icon +
			", redirect=" + redirect +
			", level=" + level +
			", isMenu=" + isMenu +
			", sort=" + sort +
			", isOpen=" + isOpen +
			"}";
	}

	/**
	 * 构建整个菜单树
	 *
	 * @author zengzr
	 */
	public static List<Menu> buildNodeTree(List<Menu> nodeList) {
		for (Menu treeNode : nodeList) {
			if (treeNode.getLevel()==1){
				List<Menu> linkedList = treeNode.findChildNodes(nodeList, treeNode.getMenuId());
				if (linkedList.size() > 0) {
					treeNode.setChildren(linkedList);
				}
			}
		}
		//清除二级菜单
		Iterator it=nodeList.iterator();
		while (it.hasNext()){
			Menu m= (Menu) it.next();
			if (m.getLevel()>1){
				it.remove();
			}
		}
		return nodeList;
	}

	/**
	 * 查询子节点的集合
	 *
	 * @author zengzr
	 */
	public static List<Menu> findChildNodes(List<Menu> nodeList, Integer parentId) {
		if (nodeList == null && parentId == null)
			return null;
		List<Menu> childrens=new ArrayList<>();
		for (Iterator<Menu> iterator = nodeList.iterator(); iterator.hasNext(); ) {
			Menu node = (Menu) iterator.next();
			// 根据传入的某个父节点ID,遍历该父节点的所有子节点
			if (node.getMenuPid() != 0 && parentId.equals(node.getMenuPid())) {
				List<Menu> linkedList = node.findAuthNodes(nodeList, node.getMenuId());
				if (linkedList.size() > 0) {
					node.setAuth(linkedList);
				}
				childrens.add(node);
			}
		}
		return childrens;
	}
	//按钮权限
	public static List<Menu> findAuthNodes(List<Menu> nodeList, Integer parentId) {
		if (nodeList == null && parentId == null)
			return null;
		List<Menu> auths=new ArrayList<>();
		for (Iterator<Menu> iterator = nodeList.iterator(); iterator.hasNext(); ) {
			Menu node = (Menu) iterator.next();
			// 根据传入的某个父节点ID,遍历该父节点的所有子节点
			if (node.getMenuPid() != 0 && parentId.equals(node.getMenuPid())) {
				auths.add(node);
			}
		}
		return auths;
	}
}
